package edu.umn.se.trap.logicChecker;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import edu.umn.se.trap.Form;
import edu.umn.se.trap.Grant;
import edu.umn.se.trap.exceptions.GrantException;

public class CheckSyntaxGrant implements Observer {

    @Override
    /**
     * Update method to check for syntax of grants
     */
    public void update(Observable o, Object arg) {
        
        Form form = (Form) arg;
        
        Vector<Grant> grants = form.getGrants();
        
        LogicChecker l1 = (LogicChecker)o;
        // Check each grant for access to DB, all percentages add up to 100
        int percentage = 0;
        for (int i = 0; i < grants.size(); i++)
        {
            Grant grant = grants.elementAt(i);
            
            percentage += grant.getPercentCharged();
            if (percentage < 0 || percentage > 100)
            {
                l1.setException(new GrantException("Error in percentages"));
            }    
        }    
        if (percentage != 100)
        {
            l1.setException(new GrantException("Error, percentage do not add up to 100%"));
        }    
    }
}